import { baseURL } from './config.js'
const request = (options) => {
	let url = baseURL + options.url;
	let header = {};
	if(uni.getStorageSync('userToken')){
		header.Token = uni.getStorageSync('userToken')
	}
	
	if(uni.getLocale() == 'zh-Hans'){
		header.Language = 'zh';
	} else if(uni.getLocale() == 'en'){
		header.Language = 'en';
	} else if(uni.getLocale() == 'ru'){
		header.Language = 'russia';
	}
	
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			header,
			timeOut: 50000,
			data: options.data,
			headers:{ "Content-Type" : "application/json;charset=utf-8" },
			method: options.method,
			success: (res) => {
				if (res.statusCode == 200) {
					//uni.hideLoading();
					resolve(res.data)
				}
				if(res.data.code == 10006){//token过期
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 1500
					})
					uni.setStorageSync('userToken', "");
					setTimeout(()=>{
						uni.redirectTo({
							url: 'pages/index/home'
						});
					},1500)
				}
				if(res.data.code == 10001){//未登录 无token
					uni.redirectTo({
						url: 'pages/index/home'
					});
				}
				if(res.data.code != 200 && res.data.code != 10023){//错误信息
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 1500
					})
					//uni.hideLoading();
				}
				reject(res.data)
			},
			fail: (res) => {
				console.log(res);
				reject(e)
				return uni.showToast({
					title: "network Error",
					icon: 'none',
					duration: 1500
				})
				
			},
			complete() {
				//uni.hideLoading();
			}
		})
	})
}

//导出
export default request